/**
 * Created by lenovo-pc on 2014/10/2.
 */
var V_login = Backbone.View.extend({
    el : "#container",

    render : function(){
        var total = [{},{},{}];
        var template = _.template($("#login").html())({ total : total });
        $(".form").html(template);
        $(this.el).find("form").attr("data-type", "login");
    },

    events : {
        "submit .form" : "loginFunc",
        "click .register_link" : "register"
    },

    "loginFunc" : function(ev){
        if (ev.target.dataset['type'] != "login") return;
        var data = $(ev.target).serializeObject(),
            std_id_filter = /\d{10}/,
            std_number_filter = /\d{5}[a-z0-9]/,
            flag = true;

        ev.preventDefault();
        if(Object.keys(data).length < 1){
            return false;
        }

        $("input").css({ border: "1px solid #ececec" });

        if (!std_id_filter.test(data.std_id)) {
            flag = false;
            $("input[name='std_id']").css({ border: "1px solid red" });
        }
        if (!std_number_filter.test(data.std_psw)) {
            flag = false;
            $("input[name='std_psw']").css({ border: '1px solid red' });
        }

        if(flag){
            var m_login = new M_login();

            m_login.set(data);

            Backbone.sync("create", m_login, {
                success : function(model){

                    console.log(model);
                    console.log(1);
                    if(model.state == 200 ){
                        var id = model.id;

                        m_user.set({id : id});

                        var location = window.location;
                        location.href = location.origin + model['nextSrc'];
                    }
                },
                error: function (obj) {
                    var errorData = jQuery.parseJSON(obj.responseText);
                    alert(errorData.error);
                    $("input[name='" + errorData.target + "']").css({ border: "1px solid red" });
                }
            });
        }
    },

    "register" : function(ev){
        ev.preventDefault();
        route.navigate('/register', { trigger : true} );
    }
});

var V_register = Backbone.View.extend({
    el : "#container",

    render: function(){
        var template = _.template($("#register").html())(),
            self = this;

        $(".form").html(template);
        $(this.el).find("form").attr("data-type", "register");
        var department_list = $(".department_list");

        department_list.on('click', "li", function(ev){

            var department_name = ev.target.innerHTML,
                department_id = ev.target.dataset['id'];

            self.NameArray.push(department_name);
            self.IdArray.push(department_id);
            console.log(self.IdArray.data);
            document.querySelector('.hidden_department').value = self.IdArray.data;
            document.querySelector('.form_department').value = self.NameArray.data.join(",");

            $(".department_list").slideUp(200);

        });

    },

    events : {
        "submit .form" : "registerFunc",
        "click .form_department" : "showList"
    },

    NameArray : (function(){
        var data = [];

        function push(value){
            for(var i = 0,len = data.length; i < len; i ++){
                if(data[i] == value){
                    data.splice(i, 1);
                    return false;
                }
            }
            data.push(value);
        }

        return {
            push : push,
            data : data
        }

    }()),
    IdArray : (function(){
        var data = [];

        function push(value){
            for(var i = 0,len = data.length; i < len; i ++){
                if(data[i] == value){
                    data.splice(i, 1);
                    return false;
                }
            }
            data.push(value);
        }

        return {
            push : push,
            data : data
        }

    }()),


    "showList" : function(ev) {
        var self = this;
        var department_list = $(".department_list");
        department_list.slideDown(200);

    },
    "registerFunc" : function(ev){
        var data = $(ev.target).serializeObject(),
            std_id_filter = /\d{10}/,
            std_number_filter = /\d{5}[a-z0-9]/,
            std_name_filter = /^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9])+$/,
            flag = true;

        if (ev.target.dataset['type'] != 'register') return false;

        ev.preventDefault();

        if(Object.keys(data).length < 1){
            return false;
        }

        data.std_department = data.std_department.split(",");

        $("input").css({ border: "1px solid #ececec" });

        if (!std_id_filter.test(data.std_id)) {
            flag = false;
            $("input[name='std_id']").css({ border: "1px solid red" });
        }
        if (!std_number_filter.test(data.std_number)) {
            flag = false;
            $("input[name='std_number']").css({ border: '1px solid red' });
        }
        if (!std_name_filter.test(data.std_name)) {
            flag = false;
            $("input[name='std_name']").css({ border: '1px solid red' });
        }

        if (flag) {
            var m_register = new M_register();

            m_register.set(data);

            Backbone.sync("create", m_register, {
                success: function (data) {
                    if (data.state == 200) {
                        alert("注册成功！");
                        route.navigate("", { trigger: true });
                    }
                },
                error: function (data) {
                    var errorObj = jQuery.parseJSON(data.responseText);
                    alert(errorObj.error);
                }
            })
        }
    }
});


var v_login = new V_login();
var v_register = new V_register();